//
//  KVCustemHomeCell.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/23.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit
import SnapKit

class KVCustemHomeCell: UITableViewCell {
    //原创的view
    let originalView = KVCustemOriginalView()
    //转发的View
    //底部View
    let footerView = KVHomeFooterView()
    
    //要跟新的约束
    var needUpdateConstraint: Constraint?
    //微博模型
    var status: KVStatuses? {
        didSet {
            originalView.status = status
            //赋值给转发VIew
            //判断是否有转发的微博
            needUpdateConstraint?.uninstall()
            let retweetedStatus = status?.retweeted_status
            if retweetedStatus == nil {
//                print("没有转发的微博")
                reweetedView.hidden = true
                //跟新约束到coustemVIew
                footerView.snp_makeConstraints(closure: { (make) -> Void in
                    needUpdateConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            } else {
                reweetedView.hidden = false
//                print("微博\(retweetedStatus?.text)")
                reweetedView.retweetedStatus = retweetedStatus
                footerView.snp_makeConstraints(closure: { (make) -> Void in
                needUpdateConstraint = make.top.equalTo(reweetedView.snp_bottom).constraint
             })
            }
        }
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置cell的contextView
        loadOriginalView()
        self.backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)

    }

  
    //设置cell的contextView
    private func loadOriginalView() {
        self.contentView.addSubview(originalView)
        contentView.addSubview(reweetedView)
        contentView.addSubview(footerView)

        //原创View的约束
        originalView.snp_makeConstraints { (make) -> Void in
//            make.edges.equalTo(self.contentView)
            make.top.equalTo(self.contentView).offset(homeCellSpace)
            make.leading.trailing.equalTo(self.contentView)
//            make.height.equalTo(CGFloat(arc4random_uniform(60) + 40))

        }
        //设置转发View的约束
       reweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.leading.equalTo(contentView)
        }
        //添加一个底部View
        footerView.snp_makeConstraints { (make) -> Void in
            //用属性记录要更新的约束
            needUpdateConstraint = make.top.equalTo(reweetedView.snp_bottom).constraint
            make.leading.trailing.equalTo(self.contentView)
            make.height.equalTo(homeFooterHeight)
        }
        //本身约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(footerView)
        }
    }

    private lazy var reweetedView: KVRetwetedStatusView = KVRetwetedStatusView()
}
